// packageA/pages/org-my-collect/index.js
const App = getApp();
let dictList = JSON.parse(JSON.stringify(App.globalData.dictTree));
const WXAPI = require("../../api/org");
const baseUrl = require("../../../api/config").dev; //这里用的是ES6的写法
var amapFile = require("../../../libs/amap-wx.130 2.js"); //如：..­/..­/libs/amap-wx.js
import config from "../../../config";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentInfo:{},
    areaInfoSelected: {
      provinceObj: {
        sCode: "340000",
        sName: "安徽省",
      },
      cityObj: {
        sCode: "",
        sName: "",
      },
      areaObj: {
        sCode: "",
        sName: "",
      },
      streetObj: {
        sCode: "",
        sName: "",
      },
      villageObj: {
        sCode: "",
        sName: "",
      },
    },
    isShowAddress: true,
    distance: [
      {
        value: 1.5,
        selected: false,
      },
      {
        value: 5,
        selected: false,
      },
      {
        value: 10,
        selected: false,
      },
      {
        value: 20,
        selected: false,
      },
      {
        value: 50,
        selected: false,
      },
      {
        value: 100,
        selected: false,
      },
    ],
    areaTitle: "安徽省",
    imgBaseUrl: `${baseUrl["imgBaseUrl"]}`,
    currentPageNum: 1,
    form: {
      PageIndex: 1,
      PageSize: 20
    },
    isShow: false,
    listIsOver: false,
    isLoading: false,
    option1: [],
    option2: [],
    option3: [],
    list: [],
    crossAxisCount: 1,
    crossAxisGap: 10,
    mainAxisGap: 10,
  },
  toOrgDetail(e) {
    // wx.navigateTo({
    //   url: `/packageA/pages/org-detail/index?id=${e.currentTarget.dataset.id}&title=${e.currentTarget.dataset.title}`,
    // });
  },
  onCalloutTap(e) {
    console.log("@@ onCalloutTap", e);
  },
  onLabelTap(e) {
    console.log("@@ labletap", e);
    this.setData({
      currentInfo: this.data.markers[e.detail.markerId-1]
    })
    const { text,sid } = this.data.currentInfo
    wx.navigateTo({
      url: `/packageA/pages/org-detail/index?id=${sid}&title=${text}`,
    });
  },
  open() {
    this.setData({
      isShowAddress: true,
    });
  },
  selecteItem1(e) {
    const { value } = e.currentTarget.dataset;
    let distance = this.data.distance;
    for (let i = 0; i < distance.length; i++) {
      if (distance[i].value == value) {
        distance[i].selected = !distance[i].selected;
      } else {
        distance[i].selected = false;
      }
    }
    let res = distance.filter((item) => {
      return item.selected;
    });
    if (res && res.length > 0) {
      this.setData({
        "form.dLongDistanc": res[0].value,
      });
    } else {
      this.setData({
        "form.dLongDistanc": "",
      });
    }
    this.setData({
      distance,
    });
  },
  selecteItem2(e) {
    const { value } = e.currentTarget.dataset;
    let option3 = this.data.option3;
    for (let i = 0; i < option3.length; i++) {
      if (option3[i].value == value) {
        option3[i].selected = !option3[i].selected;
      } else {
        option3[i].selected = false;
      }
    }
    let res = option3.filter((item) => {
      return item.selected;
    });
    if (res && res.length > 0) {
      this.setData({
        "form.sOrgNatureCode": res[0].value,
      });
    } else {
      this.setData({
        "form.sOrgNatureCode": "",
      });
    }
    this.setData({
      option3,
    });
  },
  sureAddress(e) {
    console.log("e", e);
    const {
      sProvinceCode,
      sCityCode,
      sCountryCode,
      sStreetCode,
      sCommunityCode,
    } = e.detail.obj;
    const address = e.detail.fullArea;
    this.setData({
      "form.sProvinceCode": sProvinceCode,
      "form.sCityCode": sCityCode,
      "form.sCountryCode": sCountryCode,
      "form.sStreetCode": sStreetCode,
      "form.sCommunityCode": sCommunityCode,
      areaTitle: address,
    });
    this.selectComponent("#item1").toggle();
    this.getList();
  },
  cancelAddress() {
    this.selectComponent("#item1").toggle();
  },
  reset() {
    let res1 = this.data.distance;
    let res2 = this.data.option3;
    res1.map((i) => (i.selected = false));
    res2.map((i) => (i.selected = false));
    this.setData({
      distance: res1,
      option3: res2,
      isShow: false,
      areaTitle: "安徽省",
      form: {
        PageIndex: 1,
        PageSize: 20,
        sProvinceCode: "340000",
      },
    });
    this.selectComponent("#item2").toggle();
  },
  onConfirm() {
    this.getList();
    this.selectComponent("#item2").toggle();
  },
  changeOptions1(e) {
    this.setData({
      "form.iMinBedTatol": e.detail == "0" ? "" : e.detail.split("-")[0],
      "form.iMaxBedTatol": e.detail == "0" ? "" : e.detail.split("-")[1],
    });
    this.getList();
  },
  changeOptions2(e) {
    this.setData({
      "form.dMinFee": e.detail == "0" ? "" : e.detail.split("-")[0],
      "form.dMaxFee": e.detail == "0" ? "" : e.detail.split("-")[1],
    });
    this.getList();
  },
  search(e) {
    this.setData({
      form: {
        ...this.data.form,
        sOrgName: e.detail.keyWords,
      },
    });
    this.getList();
  },
  openFilter() {
    this.setData({
      isShow: !this.data.isShow,
    });
  },
  formatOptions(arr) {
    let newArr = [
      {
        text: "全部",
        value: "0",
      },
    ];
    arr.map((item) => {
      newArr.push({
        text: item.sName,
        value: item.sValue,
      });
    });
    return newArr;
  },
  async getList() {
    this.setData({
      listIsOver: false,
      isLoading: true,
    });
    let isCustomLoading = true;
    let res = await WXAPI.getMyAppointOrg(this.data.form, isCustomLoading);
    let listArr = [];
    if (res.data.code == 0) {
      if (this.data.form.PageIndex == 1) {
        listArr = res.data.data.list;
        if (listArr && listArr.length > 0) {
          for (let i = 0; i < listArr.length; i++) {
            listArr[i].imgUrl =
              this.data.imgBaseUrl + listArr[i].sDoorheadPhoto;
          }
        }
      } else {
        listArr = this.data.list;
        for (let i = 0; i < res.data.data.list.length; i++) {
          listArr[i].imgUrl =
            this.data.imgBaseUrl + res.data.data.list[i].sDoorheadPhoto;
        }
          Array.prototype.push.apply(listArr, res.data.data.list);
      }
      this.setData({
        listIsOver: true,
      });
      this.setData({
        list: listArr,
        triggered: false,
        currentPageNum: res.data.data.list.length,
        isLoading: false,
      });
    }
  },
  onClickHide() {
    this.setData({
      isShow: false,
    });
  },
  //上拉触底
  onScrollBottom() {
    if (this.data.form.PageIndex < this.data.currentPageNum) {
      this.setData({
        "form.PageIndex": this.data.form.PageIndex + 1,
      });
      this.getList();
    } else if (
      this.data.form.PageIndex > this.data.currentPageNum ||
      this.data.form.PageIndex == this.data.currentPageNum
    ) {
      this.setData({
        listIsOver: true,
      });
    }
  },
  //下拉刷新
  async onRefresh() {
    this.setData({
      "form.PageIndex": 1,
      list: [],
    });
    this.getList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 初始化筛选项下拉数据格式
    this.setData({
      option1: this.formatOptions(
        dictList.filter((item) => item.sValue == "SearchOrgBedNums")[0].Child
      ),
      option2: this.formatOptions(
        dictList.filter((item) => item.sValue == "SearchOrgFee")[0].Child
      ),
      option3: this.formatOptions(
        dictList.filter((item) => item.sValue == "OrgInstitutionalNature")[0]
          .Child
      ),
    });
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      "form.PageIndex": 1,
      list: [],
    });
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
